<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="收费站" prop="deptId" v-if="deptId==101">
        <el-tree-select style="width: 100%;" v-model="queryParams.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }"
          value-key="id" placeholder="请选择" check-strictly />
      </el-form-item>
      <el-form-item label="月份" style="width: 308px">
        <el-date-picker v-model="queryParams.month" value-format="YYYY-MM" type="month"></el-date-picker>
      </el-form-item>
      <el-form-item label="值班日期" prop="guardDate">
        <el-date-picker clearable
          v-model="queryParams.guardDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择值班日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['coordination:guard:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['coordination:guard:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="guardList" @selection-change="handleSelectionChange">
      <el-table-column type="index"  label="序号"  width="65" align="center" />
      <el-table-column label="月份" align="center" prop="id" />
      <el-table-column label="日期" align="center" prop="guardDate" />
      <el-table-column label="值班站长" align="center" prop="leaderName" />
      <el-table-column label="收费" align="center" prop="" >
        <el-table-column label="白班" align="center" prop="chargeWhiteList" >
          <template #default="scope">
              <span v-for="(item,index) in scope.row.chargeWhiteList" :key="index">{{item.userName}},</span>
          </template>
        </el-table-column>
        <el-table-column label="夜班" align="center" prop="chargeBlackList" >
          <template #default="scope">
              <span v-for="(item,index) in scope.row.chargeBlackList" :key="index">{{item.userName}},</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="治超" align="center" >
        <el-table-column label="白班" align="center" prop="" >
          <template #default="scope">
              <span v-for="(item,index) in scope.row.excessWhiteList" :key="index">{{item.userName}},</span>
          </template>
        </el-table-column>
        <el-table-column label="夜班" align="center" prop="" >
          <template #default="scope">
              <span v-for="(item,index) in scope.row.excessBlackList" :key="index">{{item.userName}},</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="票证稽核" align="center" >
        <el-table-column label="主办" align="center" prop="" >
          <template #default="scope">
              <span v-for="(item,index) in scope.row.auditHostList" :key="index">{{item.userName}},</span>
          </template>
        </el-table-column>
        <el-table-column label="协办" align="center" prop="" >
          <template #default="scope">
              <span v-for="(item,index) in scope.row.auditJoinList" :key="index">{{item.userName}},</span>
          </template>
        </el-table-column>
      </el-table-column>

      <el-table-column label="ETC发行" align="center" prop="ETCPublicList" width="180">
        <template #default="scope">
              <span v-for="(item,index) in scope.row.ETCPublicList" :key="index">{{item.userName}},</span>
          </template>
      </el-table-column>
      <el-table-column label="办公" align="center" prop="officeList" >
          <template #default="scope">
              <span v-for="(item,index) in scope.row.officeList" :key="index">{{item.userName}},</span>
          </template>
        </el-table-column>
      <el-table-column label="维修" align="center" prop="repairList">
          <template #default="scope">
              <span v-for="(item,index) in scope.row.repairList" :key="index">{{item.userName}},</span>
          </template>
        </el-table-column>
      <el-table-column label="后勤" align="center" prop="logisticsList">
          <template #default="scope">
              <span v-for="(item,index) in scope.row.logisticsList" :key="index">{{item.userName}},</span>
          </template>
        </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['coordination:guard:edit']">人员调整</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改值班对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="guardRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="值班日期" prop="guardDate">
          <el-date-picker clearable
            v-model="form.guardDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择值班日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">值班和人员关联信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddTbCooGuardUser">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteTbCooGuardUser">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="tbCooGuardUserList" :row-class-name="rowTbCooGuardUserIndex" @selection-change="handleTbCooGuardUserSelectionChange" ref="tbCooGuardUser">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="人员" prop="userId" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.userId" placeholder="请输入人员" />
            </template>
          </el-table-column>
          <el-table-column label="值班岗位类型" prop="poleType" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.poleType" placeholder="请选择值班岗位类型">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Guard">
import { listGuard, getGuard, delGuard, addGuard, updateGuard } from "@/api/coordination/guard";
import { getCKInfo } from '@/utils/auth'
import { deptTreeSelect } from "@/api/system/user";
const { proxy } = getCurrentInstance();

const guardList = ref([]);
const tbCooGuardUserList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedTbCooGuardUser = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptId=ref(Number(getCKInfo('deptId')))
const deptOptions=ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: deptId.value,
    leaderId: null,
  },
  rules: {
    guardDate: [
      { required: true, message: "值班日期不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询值班列表 */
function getList() {
  loading.value = true;
  listGuard(queryParams.value).then(response => {
    guardList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    guardDate: null,
    leaderId: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  tbCooGuardUserList.value = [];
  proxy.resetForm("guardRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加值班";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getGuard(_id).then(response => {
    form.value = response.data;
    tbCooGuardUserList.value = response.data.tbCooGuardUserList;
    open.value = true;
    title.value = "修改值班";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["guardRef"].validate(valid => {
    if (valid) {
      form.value.tbCooGuardUserList = tbCooGuardUserList.value;
      if (form.value.id != null) {
        updateGuard(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addGuard(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除值班编号为"' + _ids + '"的数据项？').then(function() {
    return delGuard(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 值班和人员关联序号 */
function rowTbCooGuardUserIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 值班和人员关联添加按钮操作 */
function handleAddTbCooGuardUser() {
  let obj = {};
  obj.userId = "";
  obj.poleType = "";
  tbCooGuardUserList.value.push(obj);
}

/** 值班和人员关联删除按钮操作 */
function handleDeleteTbCooGuardUser() {
  if (checkedTbCooGuardUser.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的值班和人员关联数据");
  } else {
    const tbCooGuardUsers = tbCooGuardUserList.value;
    const checkedTbCooGuardUsers = checkedTbCooGuardUser.value;
    tbCooGuardUserList.value = tbCooGuardUsers.filter(function(item) {
      return checkedTbCooGuardUsers.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleTbCooGuardUserSelectionChange(selection) {
  checkedTbCooGuardUser.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('coordination/guard/export', {
    ...queryParams.value
  }, `guard_${new Date().getTime()}.xlsx`)
}

getList();
getDeptTree()
</script>
